<?php 
	namespace app\admin\controller;
	use think\Db;
	class Nav extends Base{
		public function index(){
			return $this->fetch();
		}
		public function navlist(){
			//获取所有产品
			$nav=Db::name('nav_list')->order('nav_id','asc')->paginate(7,false, [ 'type'=>'Bootstrap2' ]);
			// var_dump($nav);die;
			$navlist=$nav->items();
			//编号
			$current=$nav->currentPage();
			$limit=$nav->listRows();
			$con=($current-1)*$limit;
			$a=$con+1;
			$this->assign('a',$a);
			$this->assign('navlist',$navlist);
			$this->assign("nav",$nav);
			return $this->fetch();
		}
		public function navadd(){
			$nav=Db::name('nav_list')->select();
			$this->assign('nav',$nav);
			//添加功能
			if($_POST){
				$data=array(
					'nav_name'=>$this->request->post('nav_name'),
					'nav_url'=>$this->request->post('nav_url'),
					'parent_id'=>$this->request->post('parent_id'),
				);
				//执行添加功能
				$res=Db::name('nav_list')->insert($data);
				if($res){
					$this->success('添加成功','/admin/nav/navlist');
				}else{
					$this->error('数据执行有误');
				}
			}
			return $this->fetch();
		}
		public function navedit($pid){
			//编辑功能
			$nav=Db::name('nav_list')->where("nav_id",$pid)->find();
			$this->assign('nav',$nav);
			//导航父级
			$navlist=Db::name('nav_list')->select();
			$this->assign('navlist',$navlist);
			if($_POST){
				$data=array(
					'nav_name'=>$this->request->post('nav_name'),
					'nav_url'=>$this->request->post('nav_url'),
					'parent_id'=>$this->request->post('parent_id'),
				);
				//接收隐藏域
				$nav_id=$this->request->post('nav_id');
				//执行编辑功能
				$res=Db::name('nav_list')->where("nav_id",$nav_id)->update($data);
				if($res){
					$this->success('编辑成功','/admin/nav/navlist');
				}else{
					$this->error('数据执行有误');
				}
			}
			return $this->fetch();
		}
		public function navdel($pid){
			//执行删除功能
			$res=Db::table('nav_list')->where("nav_id",$pid)->delete();
			if($res){
				$this->success('删除成功','/admin/nav/navlist');
			}else{
				$this->error('数据执行有误');
			}
		}
	}